package Q2;

import java.util.*;

class Process {
    String name; // 进程名
    int arrivalTime; // 到达时间
    int burstTime; // 所需运行时间
    int remainingTime; // 剩余时间
    int finishTime; // 完成时间
    boolean isFinish = false; // 是否完成

    public Process(String name, int arrivalTime, int burstTime) {
        this.name = name;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
        this.remainingTime = burstTime;
    }
}

public class Question2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Process> processList = new ArrayList<>();

        // 输入4行数据
        for (int i = 0; i < 4; i++) {
            String input = scanner.nextLine();
            String[] parts = input.split(" ");
            String name = parts[0];
            int arrivalTime = Integer.parseInt(parts[1]);
            int burstTime = Integer.parseInt(parts[2]);
            processList.add(new Process(name, arrivalTime, burstTime));
        }

        // SRTF调度算法
        StringBuilder executionOrder = new StringBuilder();
        int currentTime = 0;
        int remainingProcesses = processList.size();

        while (remainingProcesses > 0) {
            // 找到下一个要执行的进程
            Process currentProcess = null;
            for (Process p : processList) {
                if (p.arrivalTime <= currentTime && !p.isFinish) {
                    if (currentProcess == null || p.remainingTime < currentProcess.remainingTime ||
                            (p.remainingTime == currentProcess.remainingTime && p.arrivalTime < currentProcess.arrivalTime)) {
                        currentProcess = p;
                    }
                }
            }

            if (currentProcess != null) {
                executionOrder.append(currentProcess.name).append(" ");
                // 执行该进程
                currentProcess.remainingTime--;
                if (currentProcess.remainingTime == 0) {
                    currentProcess.isFinish = true;
                    currentProcess.finishTime = currentTime + 1; // 加1是因为从currentTime开始处理
                    remainingProcesses--;
                }
            }
            currentTime++;
        }

        // 输出执行顺序
        System.out.println(executionOrder.toString().trim());
        scanner.close();
    }
}